<template>
  <main-page>
    <div slot="body">
      <el-form :inline="true" :model="search" size="mini">
        <el-form-item>
          <el-button type="danger" @click="dialogFormVisible = true;">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%" tooltip-effect="dark">
        <el-table-column prop="id" label="编号" width="80"></el-table-column>
        <el-table-column prop="chapterName" label="目录名称"></el-table-column>
        <el-table-column label="是否免费">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.costGold == 0">免费</el-tag>
            <el-tag type="danger" v-else>￥ {{scope.row.costGold}} 金币</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">{{scope.row.createTime | datetime}}</template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              title="编辑"
              size="small"
              @click="handleClick2EditById(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              title="删除"
              size="small"
              @click="handleClick2DeleteById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination :listNum="listNum" @currentChange="handleCurrentChange"></pagination>

      <el-dialog title="目录管理" :visible.sync="dialogFormVisible" width="40%">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm">
          <el-form-item label="目录名称" prop="chapterName">
            <el-input v-model="form.chapterName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="需要金币">
            <el-input v-model="form.costGold" autocomplete="off" placeholder="金币为0 视为免费"></el-input>
          </el-form-item>
          <el-form-item label="内容" prop="chapterContentVo.content">
            <el-input
              type="textarea"
              :rows="5"
              v-model="form.chapterContentVo.content"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="onSubmit">提交</el-button>
        </div>
      </el-dialog>
    </div>
  </main-page>
</template>

<script>
import {
  getChapterList,
  saveChapter,
  deleteChapter,
  updateChapterById
} from "api/ChapterApi";
export default {
  name: "ChapterList",
  props: {
    booksId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      form: {
        id: "",
        bookId: this.booksId,
        chapterName: "",
        costGold: 0,
        chapterContentVo: {
          id: "",
          chapterId: "",
          bookId: this.booksId,
          content: ""
        }
      },
      rules: {
        chapterName: [{ required: true, message: "请输入目录名称" }],
        'chapterContentVo.content': [{required: true, message: "请输入内容"}]
      },
      tableData: [],
      listNum: 0,
      search: {
        currentIndex: 1,
        bookId: this.booksId
      }
    };
  },
  methods: {
    getChapterList() {
      getChapterList(this.search).then(res => {
        if (res) {
          this.tableData = res.datas;
          this.listNum = res.pagination.totalCount;
        }
      });
    },
    handleCurrentChange(page) {
      this.search.currentIndex = page;
      this.getChapterList();
    },
    handleClick2EditById(row) {
      console.info(row);
      this.dialogFormVisible = true;
      this.$base.set2Form(this.form, row);
    },
    handleClick2DeleteById(id) {
      this.$base.deleteConfirm(() => {
        deleteChapter({ id }).then(res => {
          if (res) {
            this.$base.deleteOk();
            this.getChapterList();
          }
        });
      });
    },
    onSubmit() {
      this.form.bookId = this.booksId;
      this.form.chapterContentVo.bookId = this.booksId;

  console.info(this.form);

      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id) {
            //修改
            updateChapterById(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getChapterList();
              }
            });
          } else {
            //保存
            saveChapter(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getChapterList();
              }
            });
          }
        }
      });
    }
  },
  created() {
    this.getChapterList();
  },
  watch: {
    dialogFormVisible(newDialogFormVisible) {
      if (!newDialogFormVisible) {
        this.$base.clearObj(this.form);
      }
    },
  }
};
</script>
<style lang="stylus" scoped></style>
